package State;
//状态模式

public class StatePatternClient {
    public static void main(String[] args) {
        Context context = new Context();
        context.Handle();
        context.Handle();
        context.Handle();
        context.Handle();
    }
}
//环境类：用于根据状态执行不同的行为
class Context{
    private State state;
//定义环境类的初始状态
    public Context() {
        this.state = new ConcreteStateA();
    }
    //设置与获取状态


    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
    public void Handle(){
        state.Handle(this);//对请求做处理
    }
}
//抽象状态类
abstract class State{
    public abstract void Handle(Context context);
}
//具体状态
class ConcreteStateA extends State{

    @Override
    public void Handle(Context context) {
        System.out.println("当前的状态为：A");
        context.setState(new ConcreteStateB());
    }
}
class ConcreteStateB extends State{

    @Override
    public void Handle(Context context) {
        System.out.println("当前的状态为：B");
        context.setState(new ConcreteStateA());
    }
}

